<?php
namespace Home\Controller;

use Lib\DataTable;
use Think\Controller;
use Think\Verify;

class IndexController extends Controller {
    public function index(){
        if (I('param.draw')) {
            if (!check_self_phone(I('param.phone'))){
                show(0,'手机号填写不正确');
            }
            if (!check_phone_verify(I('param.verify'))){
                show(0,'验证码填写不正确');
            }
            $DataTable = new DataTable(D('CardLog'));

            $DataTable->filter = ['cno', 'cpwd'];
            $DataTable->map['uphone']=I('param.phone');
            $DataTable->lists();
            $DataTable->returnJson();
        }
        if ($_SERVER['HTTP_REFERER']!=='http://www.kvvgame.com/admin.php/Gcard.html' && !APP_DEBUG){
            exit('您没有权限访问');
        }

        $this->display();
    }
    public function getCard(){
        if (IS_POST){
            $gid=I('post.gid');
            $game_id=I('post.game_id');
            $phone=I('post.phone');
            $num=I('post.num');
            $verify=I('post.verify');
            $verifyImg=I('post.verifyImg');

            if(!is_numeric($num) || $num < 1){
                show(0,'提取张数填写错误！' );
            }
            if (!check_img_verify($verifyImg)){
                show(0,'图形验证码错误' );
            }
            
            if(M('Game')->find($gid)){
                if (check_self_phone($phone) && check_phone_verify($verify)){

                    $User=D('User');
                    $UserData=$User->getUserByPhone($phone);
                    $unfinished=M('CardLog')->where("uid=%d and status=0",$UserData['id'])->count();
                    $allow=M('User')->where("id='%s'",$UserData['id'])->getField('num');

                    if ($unfinished+$num>$allow){
                        show(0,'您最高可拿'.$allow.'张卡。当前还有'.$unfinished.'张卡未结算。领取张数已超出限额，请结清欠款后再进行取卡。');
                    }

                    $Card=M('Card');
                    $CardLog=M('CardLog');


                    $Card->startTrans();
                    $CardLog->startTrans();

                    $CardList=$Card->lock(true)->where("status=0 and gid=%d and (expire_time IS NULL or expire_time>'%s')",$gid,date("Y-m-d H:i:s"))->limit($num)->select();
                    $Flag=false;
                    $dataList=array();
                    $txt="";
                    if ($CardList && sizeof($CardList)>=$num){
                        foreach ($CardList as $i=>$CardData){
                            $CardList[$i]['status']=1;

                            $dataList[$i]['uid']=$UserData['id'];
                            $dataList[$i]['uname']=$UserData['name'];
                            $dataList[$i]['uphone']=$UserData['phone'];
                            $dataList[$i]['game_id']=$game_id;
                            $dataList[$i]['cno']=$CardData['cno'];
                            $dataList[$i]['cpwd']=$CardData['cpwd'];
                            $dataList[$i]['game']=D('Game')->getNameById($gid);
                            $dataList[$i]['amount']=$CardData['amount'];
                            $dataList[$i]['price']=$CardData['price'];
                            $dataList[$i]['status']=0;

                            $txt.=$dataList[$i]['cno'].' '.$dataList[$i]['cpwd']."\r\n";

                            if($Card->save($CardList[$i])){
                                $Flag=true;
                            }else{
                                $Flag=false;
                                break;
                            }
                        }

                        if ($CardLog->addAll($dataList) && $Flag){
                            $Card->commit();
                            $CardLog->commit();
                            show(1,"取卡成功,请妥善保存。",array('table'=>$dataList,'txt'=>base64_encode($txt)));
                        }else{
                            $Card->rollback();
                            $CardLog->rollback();
                            show(0,'取卡失败，请重试');
                        }
                    }else{
                        $Card->rollback();
                        $CardLog->rollback();
                        show(0,'取卡失败，很遗憾，目前系统库存为'.sizeof($CardList).'不足以出库，请明天再来');
                    }


                }else{
                    show(0,'手机验证码错误');
             }
            }else{
                show(0,'没有该游戏的点卡');
            }
        }
        $GameList=M('Game')->where("status=1")->select();
        $this->assign('GameList',$GameList);
        $this->display('get-card');
    }

    public function verifyImg(){
        ob_clean();
        $Verify=new Verify(array('length'=>3,'bg'=>array(200,200,200)));
        $Verify->entry();
    }

    public function Verify($phone,$id=''){
        $uid=D('User')->getUidByPhone($phone);
        if ($uid){
//            发送验证码时验证
            session('Verify'.$id.'.Phone',$phone);
            session('Verify'.$id.'.Timeout',time()+300);
            session('Verify'.$id.'.Verify',rand_str());
            $Verify=session('Verify'.$id.'.Verify');
            send_sms($phone, '您的验证码为'.$Verify.'，打死也不要告诉其他人！', 'KvvCard_getVerify');
        }else{
            show(0,'该手机号还为开通此项业务，如需开通，请联系本网站客服');
        }

    }
    
}
